<?php


namespace app\common\service\auth;


use app\model\Agent;
use think\facade\Log;

class AuthYmshService extends AuthBase implements AuthInterface
{
    protected static $_loginInfo;

    const SCENE_ADMIN = 1;//总后台
    const SCENE_MER = 2;//商户

    public function login($params)
    {
        if ($msg = dmjValidate([
            'scene' => 'require',
            'merchant_id' => 'requireIf:scene,' . self::SCENE_MER,
            'merchant_name' => 'requireIf:scene,' . self::SCENE_MER,
        ], [], $params)) {
            return fail($msg);
        }
        Log::info($params);

        $str = time() . rand(0, 999999);
        $token = base64_encode($str . $str . $str);

        cache($token, [
            'scene' => input('scene'),
            'merchant_id' => $params['merchant_id'] ?? 0,
            'merchant_name' => $params['merchant_name'] ?? 0,
        ], 3600 * 12 * 30);

        return success('success', compact('token'));
    }

    /**
     * 验证
     * @param $params
     * @return bool|mixed|object|\think\App
     */
    public function attempt($params)
    {
        //验证token登录
        $token = request()->header('Authorization');

        $token = $token ?? ($params['token'] ?? '');
        if (empty($token)) {
            Log::info('token为空请求');
            return false;
        }

        $info = cache($token);

        if (!$info) {
            Log::info('token已过期');
            return false;
        }
        self::$_loginInfo = $info;

        return $info;
    }

    //java后端登录
    public function loginBySign()
    {
        self::$_loginInfo = [
            'scene' => self::SCENE_ADMIN,
        ];
    }

    public function id()
    {
        return self::$_loginInfo['merchant_id'] ?? 0;
    }

    /**
     * @return array
     * merchant_id
     * merchant_name
     */
    public function user()
    {
        return self::$_loginInfo;
    }


    public function merchant()
    {
        return self::$_loginInfo;
    }

    public function merchantId()
    {

        return self::$_loginInfo['merchant_id'] ?? 0;

    }

    public function isAdmin()
    {

        $scene = self::$_loginInfo['scene'] ?? 0;

        return $scene == self::SCENE_ADMIN;
    }

    /**
     * 是否为商户
     * @return bool
     */
    public function isMerchant()
    {

        $scene = self::$_loginInfo['scene'] ?? 0;
        return $scene == self::SCENE_MER;

    }

    public function isLogin()
    {
        return !empty(self::$_loginInfo);
    }

    /**
     * 有美商户对应的所有已绑定的代理id
     * @return array
     */
    public function allBindAgentIds()
    {
        $cacheKey = 'allBindAgentIds__' . $this->merchantId();
        //总后台
        if ($this->isAdmin()) {
            return Agent::where('merchant_id', '<>', null)
                ->where('is_relation', 1)
                ->cache($cacheKey, 2)
                ->column('id');
        }

        //商户后台
        return Agent::where('merchant_id', $this->merchantId())
            ->cache($cacheKey, 2)
            ->column('id');

    }


}